<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级" prop="className">
        <el-input v-model="queryParams.className" placeholder="请输入班级" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生" prop="studentName">
        <el-input v-model="queryParams.studentName" placeholder="请输入学生" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="跟踪日期" prop="trackDate">
        <el-date-picker clearable v-model="queryParams.trackDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择跟踪日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="追踪目的" prop="trackPurpose">
        <el-input v-model="queryParams.trackPurpose" placeholder="请输入追踪目的" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="追踪备注" prop="trackRemark">
        <el-input v-model="queryParams.trackRemark" placeholder="请输入追踪备注" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="追踪人" prop="trackMan">
        <el-input v-model="queryParams.trackMan" placeholder="请输入追踪人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生状态" prop="studentStatus">
        <el-select v-model="queryParams.studentStatus" placeholder="请选择学生状态" clearable>
          <el-option v-for="dict in dict.type.sys_student_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="追踪次数" prop="trackCount">
        <el-input v-model="queryParams.trackCount" placeholder="请输入追踪次数" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生id" prop="studentId">
        <el-input v-model="queryParams.studentId" placeholder="请输入学生id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="班级id" prop="classId">
        <el-input v-model="queryParams.classId" placeholder="请输入班级id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['employment:track:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['employment:track:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['employment:track:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['employment:track:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生跟踪表" align="center" prop="id" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="学生" align="center" prop="studentName" />
      <el-table-column label="跟踪日期" align="center" prop="trackDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.trackDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="追踪目的" align="center" prop="trackPurpose">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_track_purpose" :value="scope.row.trackPurpose" />
        </template>
      </el-table-column>
      <el-table-column label="追踪备注" align="center" prop="trackRemark" />
      <el-table-column label="追踪人" align="center" prop="trackMan" />
      <el-table-column label="学生状态" align="center" prop="studentStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_student_status" :value="scope.row.studentStatus" />
        </template>
      </el-table-column>
      <el-table-column label="追踪次数" align="center" prop="trackCount" />
      <!-- <el-table-column label="学生id" align="center" prop="studentId" /> -->
      <el-table-column label="班级id" align="center" prop="classId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['employment:track:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['employment:track:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改学生跟踪对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择班级" prop="className">
          <el-select v-model="form.className" @change="changOne" placeholder="请选择班级名" style="width: 300px;">
            <el-option v-for="item in classOptions" :key="item.id" :label="item.className" :value="item">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="学生" prop="studentName">
          <el-select v-model="selectedStudents" multiple v-if="stundentList && stundentList.length > 0">
            <el-option v-for="student in stundentList" :key="student.id" :label="student.studentName" :value="student.studentName">
              {{ student.studentName }}
            </el-option>
          </el-select>
          <p v-else>暂无数据...</p>
        </el-form-item>

        <el-form-item label="跟踪日期" prop="trackDate">
          <el-date-picker clearable v-model="form.trackDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择跟踪日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="追踪目的" prop="trackPurpose">
          <el-select v-model="form.trackPurpose" placeholder="请选择追踪目的">
            <el-option v-for="(option, index) in trackPurposesOptions" :key="index" :label="option.label"
              :value="option.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="追踪人" prop="trackMan">
          <el-input v-model="form.trackMan" placeholder="请输入追踪人" />
        </el-form-item>
        <el-form-item label="追踪次数" prop="trackCount">
          <el-input v-model="form.trackCount" placeholder="请输入追踪次数" />
        </el-form-item>
        <el-form-item label="学生状态" prop="studentStatus">
          <el-select v-model="form.studentStatus" placeholder="请选择学生状态">
            <el-option v-for="(status, index) in studentStatusOptions" :key="index" :label="status.label"
              :value="status.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="追踪备注" prop="trackRemark">
          <el-input v-model="form.trackRemark" placeholder="请输入追踪备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getClassIdByStudent, listTrack, getTrack, delTrack, addTrack, updateTrack } from "@/api/employment/track";
import { listClass } from "@/api/student/class";
export default {
  name: "Track",
  dicts: ['sys_student_status', 'sys_track_purpose'],
  data() {
    return {
      // 选择的学生name集合
      selectedStudents: [],
      stundentList: {
        id: null,
        studentName: null,
      },
      studentStatusOptions: [
        { label: '在读未毕业', value: 0 },
        { label: '留级在读', value: 1 },
        { label: '毕业未就业', value: 2 },
        { label: '已就业', value: 3 }
      ],
      trackPurposesOptions: [
        { label: '简历修改', value: 0 },
        { label: '录音分析', value: 1 },
        { label: '就业指导', value: 2 }
      ],
      classOptions: {
        id: null,
        className: null,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生跟踪表格数据
      trackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: null,
        studentName: null,
        trackDate: null,
        trackPurpose: null,
        trackRemark: null,
        trackMan: null,
        studentStatus: null,
        trackCount: null,
        studentId: null,
        classId: null
      },
      queryParamsTwo: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        classes: null,
        campus: null,
        status: null,
        phone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    console.log(1111111111111111)
    this.getList();
    console.log(222)
  },
  methods: {
    // 选择的班级

    changOne(ValueStudent) {
      this.form.classId = ValueStudent.id;
      this.form.className = ValueStudent.className;
      // 根据班级的id去查所有的学生,然后多选
      getClassIdByStudent(this.form.classId).then(response => {
        const students = response.rows;
        this.$set(this, 'stundentList', students);
        console.log(response.rows);
      });

    },
    /** 查询学生跟踪列表 */
    getList() {
      this.loading = true;
      listTrack(this.queryParams).then(response => {
        this.trackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        className: null,
        studentName: null,
        trackDate: null,
        trackPurpose: null,
        trackRemark: null,
        trackMan: null,
        studentStatus: null,
        trackCount: null,
        studentId: null,
        classId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生跟踪";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTrack(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生跟踪";
      });
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTrack(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.studentName = this.selectedStudents.join(',');
            addTrack(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生跟踪编号为"' + ids + '"的数据项？').then(function () {
        return delTrack(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('employment/track/export', {
        ...this.queryParams
      }, `track_${new Date().getTime()}.xlsx`)
    }
  },
  mounted() {
    console.log("==============");
    // 页面加载完成后触发的方法
    listClass(this.queryParamstwo).then(response => {
      this.classOptions = response.rows;
      console.log(this.classOptions);
      this.total = response.total;
      this.loading = false;
    });
  },
};
</script>
